package com.neutech.maplestory.entity;

import com.neutech.maplestory.client.MapleStoryClient;
import com.neutech.maplestory.constant.Constant;
import com.neutech.maplestory.util.ImageUtil;

import java.awt.*;

/**
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/11/27 10:12
 */
public class Background extends AbstractMapleStoryObject {

    private static final int bgHeight = 200;

    {
        images = new Image[14];
        for (int i=0;i<14;i++){
            images[i] = ImageUtil.getImageByKey("bg_drop" + i);
        }
    }

    public Background(MapleStoryClient msc){
        this.msc = msc;
        this.img = ImageUtil.getImageByKey("bg");
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
        this.speed = Constant.HERO_SPEED;
    }

    public Background(MapleStoryClient msc,int x,int y){
        this(msc);
        this.x = x;
        this.y = y;
    }
    private int step;
    @Override
    public void draw(Graphics g) {
        move();
        // 按图层的顺序画
        sky.draw(g);
        cloud1.draw(g);
        cloud12.draw(g);
        hill1.draw(g);
        cloud2.draw(g);
        cloud22.draw(g);
        hill2.draw(g);
        lake.draw(g);
        hill3.draw(g);
        // 前景
        g.drawImage(img,x,y,null);
        // 画落叶动画
        /*if(step > 13){
            step = 0;
        }
        g.drawImage(images[step++],x + 50 ,y + 570,null);*/

    }

    @Override
    public void move() {
        // 第一张云
        if(cloud1.x <= -2550){
            cloud1.x = 2550;
        }
        if(cloud12.x <= -2550){
            cloud12.x = 2550;
        }
        // 第二张云
        if(cloud2.x <= -2368){
            cloud2.x = 2368;
        }

        if(cloud22.x <= -2368){
            cloud22.x = 2368;
        }
        // 人的方向是Direction.RIGHT
        // 背景朝左走
        boolean leftWalk = msc.hero.right && msc.hero.x >= Constant.WINDOW_WIDTH / 2 + 50 ;
        if(leftWalk){
            this.x -= Constant.HERO_SPEED;
            msc.hero.speed = 0;
            // 什么时候恢复人物的速度
            if(this.x <= Constant.WINDOW_WIDTH - this.width + rightBound + 50){
                this.x = Constant.WINDOW_WIDTH - this.width + rightBound + 50;
                msc.hero.speed = Constant.HERO_SPEED;
            }
        }

        boolean rightWalk = msc.hero.left && msc.hero.x <= Constant.WINDOW_WIDTH / 2 + 50;
        if(rightWalk){
            this.x += Constant.HERO_SPEED;
            msc.hero.speed = 0;
            // 什么时候恢复人物的速度
            if(this.x >= leftBound){
                this.x = leftBound;
                msc.hero.speed = Constant.HERO_SPEED;
            }
        }

    }

    /**
     * 右边界
     */
    private final int rightBound = 50;
    /**
     * 左边界
     */
    private final int leftBound = -20;

    private final Lake lake = new Lake();

    private final Sky sky = new Sky();

    private final Cloud cloud1 = new Cloud(1);

    private final Cloud cloud12 = new Cloud(1,2550);

    private final Hill hill1 = new Hill(1);

    private final Cloud cloud2 = new Cloud(2);

    private final Cloud cloud22 = new Cloud(2,2368);

    private final Hill hill2 = new Hill(2);

    private final Hill hill3 = new Hill(3);
    /**
     * 所有的背景元素使用成员内部类加载
     */
    class Lake extends AbstractMapleStoryObject {
        public Lake(){
            this.y = 778;
            this.img = ImageUtil.getImageByKey("lake");
        }

        @Override
        public void draw(Graphics g) {
            for (int i = 0; i < 112; i++) {
                g.drawImage(img,x + (i * 22),y-bgHeight,null);
            }
        }
    }



    /**
     * 天空的内部类
     */
    static class Sky extends AbstractMapleStoryObject {
        public Sky(){
            this.img = ImageUtil.getImageByKey("back0");
        }

        @Override
        public void draw(Graphics g) {
            for(int i=0;i<112;i++){
                g.drawImage(img,x + (i * 22),y ,null);
            }
        }
    }

    static class Cloud extends AbstractMapleStoryObject {
        private int type;

        public Cloud(int type,int x){
            this(type);
            this.x = x;
        }
        /**
         *
         * @param type 表示哪种云
         */
        public Cloud(int type){
            this.type = type;
            switch (type){
                case 1:
                    this.y = 346;
                    this.speed = 1;
                    this.img = ImageUtil.getImageByKey("back1");
                    break;
                case 2:
                    this.y = 505;
                    this.speed = 3;
                    this.img = ImageUtil.getImageByKey("back3");
                    break;
                default:
                    break;
            }
        }

        @Override
        public void draw(Graphics g) {
            move();
            switch (type){
                case 1:
                    for(int i=0;i<3;i++){
                        g.drawImage(img,x + (i * 850),y - bgHeight,null);
                    }
                    break;
                case 2:
                    for (int i = 0; i < 4; i++) {
//                        g.fillRect(x + (i * 592),y - bgHeight,592,98);
                        g.drawImage(img,x + (i * 592),y - bgHeight,null);
                    }
                    break;
                default:
                    break;
            }
        }
        @Override
        public void move() {
            this.x -= speed;
        }
    }

    class Hill extends AbstractMapleStoryObject {
        private int type;
        public Hill(int type){
            this.type = type;
            switch (type){
                case 1:
                    this.y = 457;
                    this.img = ImageUtil.getImageByKey("back2");
                    break;
                case 2:
                    this.y = 536;
                    this.img = ImageUtil.getImageByKey("back4");
                    break;
                case 3:
                    this.y = 571;
                    this.img = ImageUtil.getImageByKey("back5");
                    break;
                default:
                    break;
            }
        }

        @Override
        public void draw(Graphics g) {
            switch (type){
                case 1:
                    for (int i = 0; i < 4; i++) {
                        g.drawImage(img,x + (i * 630),y - bgHeight,null);
                    }
                    break;
                case 2:
                    for (int i = 0; i < 2; i++) {
                        g.drawImage(img,x + (i * 973),y - bgHeight,null);
                    }
                    break;
                case 3:
                    g.drawImage(img,x,y - bgHeight - 20,null);
                    break;
                default:
                    break;
            }
        }
    }
}
